package com.ruoyi.project.setsystem.companydept.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.setsystem.companydept.domain.CompanyDept;
import com.ruoyi.project.setsystem.companydept.service.ICompanyDeptService;
import com.ruoyi.project.setsystem.rolepower.domain.RolePower;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 大帅比
 * @date 2020-09-19 10:52
 * @desc 百战程序员 http://www.itbaizhan.com
 */
@Controller
@RequestMapping("/setsystem/companydept")
public class ComDeptController extends BaseController {

        private String prefix = "setsystem/companydept";
        @Autowired
        private ICompanyDeptService companyDeptService;
        @RequiresPermissions("setsystem:companydept:view")
        @GetMapping()
        public String companydept(){

                return prefix+"/companydept";
//                return prefix+"/dcfsf";
        }


        @RequiresPermissions("setsystem:companydept:list")
        @PostMapping("/list")
        @ResponseBody
        public List<CompanyDept> list(CompanyDept companyDept) {
                List<CompanyDept> deptList = companyDeptService.All(companyDept);
                return deptList;
        }
//        public TableDataInfo demo01(CompanyDept companyDept){
//                startPage();
//                List<CompanyDept> all = companyDeptService.All(companyDept);
//                System.out.println(all);
//                return getDataTable(all);
//        }


        @RequestMapping("/add")
        public String add()
        {
                System.out.println("qqqqq");
                return prefix + "/add";
        }

        /**
         * 新增保存部门
         */
        @Log(title = "部门管理", businessType = BusinessType.INSERT)
        @RequiresPermissions("setsystem:companydept:add")
        @PostMapping("/add")
        @ResponseBody
        public AjaxResult addSave(@Validated CompanyDept companyDept)
        {
                if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(companyDeptService.checkDeptNameUnique(companyDept)))
                {
                        return error("新增部门'" + companyDept.getDeptName() + "'失败，部门名称已存在");
                }
                return toAjax(companyDeptService.insertDept(companyDept));
        }

        /**
         * 修改
         */
        @GetMapping("/edit/{deptId}")
        public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap)
        {
                CompanyDept companyDept = companyDeptService.selectDeptById(deptId);
                if (StringUtils.isNotNull(companyDept) && 100L == deptId)
                {
                        companyDept.setParentName("无");
                }
                mmap.put("companydept", companyDept);
                return prefix + "/edit";
        }

        /**
         * 保存
         */
        @Log(title = "部门管理", businessType = BusinessType.UPDATE)
        @RequiresPermissions("setsystem:companydept:edit")
        @PostMapping("/edit")
        @ResponseBody
        public AjaxResult editSave(@Validated CompanyDept companyDept)
        {
                if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(companyDeptService.checkDeptNameUnique(companyDept)))
                {
                        return error("修改部门'" + companyDept.getDeptName() + "'失败，部门名称已存在");
                }
                else if(companyDept.getParentId().equals(companyDept.getDeptId()))
                {
                        return error("修改部门'" + companyDept.getDeptName() + "'失败，上级部门不能是自己");
                }
                return toAjax(companyDeptService.updateDept(companyDept));
        }

        /**
         * 删除
         */
        @Log(title = "部门管理", businessType = BusinessType.DELETE)
        @RequiresPermissions("setsystem:companydept:remove")
        @GetMapping("/remove/{deptId}")
        @ResponseBody
        public AjaxResult remove(@PathVariable("deptId") Long deptId)
        {
                if (companyDeptService.selectDeptCount(deptId) > 0)
                {
                        return AjaxResult.warn("存在下级部门,不允许删除");
                }
                if (companyDeptService.checkDeptExistUser(deptId))
                {
                        return AjaxResult.warn("部门存在用户,不允许删除");
                }
                return toAjax(companyDeptService.deleteDeptById(deptId));
        }

        /**
         * 校验部门名称
         */
        @PostMapping("/checkDeptNameUnique")
        @ResponseBody
        public String checkDeptNameUnique(CompanyDept companyDept)
        {
                return companyDeptService.checkDeptNameUnique(companyDept);
        }

        /**
         * 选择部门树
         */
        @GetMapping("/selectDeptTree/{deptId}")
        public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap mmap)
        {
                mmap.put("companydept", companyDeptService.selectDeptById(deptId));
                return prefix + "/tree";
        }

        /**
         * 加载部门列表树
         */
        @GetMapping("/treeData")
        @ResponseBody
        public List<Ztree> treeData()
        {
                List<Ztree> ztrees = companyDeptService.selectDeptTree(new CompanyDept());
                return ztrees;
        }

        /**
         * 加载角色部门（数据权限）列表树
         */
        @GetMapping("/roleDeptTreeData")
        @ResponseBody
        public List<Ztree> deptTreeData(RolePower rolePower)
        {
                List<Ztree> ztrees = companyDeptService.roleDeptTreeData(rolePower);
                return ztrees;
        }

}
